package com.gin.fanbox.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

/**
 * @author bx002
 * @since 2024/2/5 17:16
 */
public class JacksonUtils {

    public static final ObjectMapper MAPPER = new ObjectMapper()
            .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
            .enable(SerializationFeature.INDENT_OUTPUT)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)
            .registerModule(new JavaTimeModule());


    /**
     * 获取泛型的Collection Type
     *
     * @param containerClass 容器类型
     * @param elementClasses 泛型类型
     * @return JavaType Java类型
     * @since 1.0
     */
    public static JavaType obtainJavaType(Class<?> containerClass, Class<?>... elementClasses) {
        return MAPPER.getTypeFactory().constructParametricType(containerClass, elementClasses);
    }

    public static void printPretty(Object object) throws JsonProcessingException {
        System.out.println(MAPPER.writeValueAsString(object));
    }
}
